package net.ib.mtalk.network.task;

import net.ib.mntalk.protocol.Common;
import net.ib.mntalk.protocol.Session.AddBlockUsersReq;
import net.ib.mntalk.protocol.Session.AddBlockUsersRes;
import net.ib.mntalk.protocol.Session.SetBuddyInfoReq;
import net.ib.mntalk.protocol.Session.SetBuddyInfoRes;
import net.ib.mtalk.constant.HandlerMsgConst;
import net.ib.mtalk.database.helper.BuddyDatabaseHelper;
import net.ib.mtalk.network.TcpEnvelope;
import net.ib.mtalk.network.MethodId.Session;
import net.ib.mtalk.util.MTalkLogWriter;
import android.content.Context;
import android.os.Handler;

public class SetRemoveBuddyTask extends AsyncNetTask {
	
	private SetBuddyInfoReq mSetBuddyInfoReq;
	
	public SetRemoveBuddyTask(Context _context, Handler _uiHandler, SetBuddyInfoReq setBuddyInfoReq) {
		super(_context, _uiHandler);
		this.mSetBuddyInfoReq = setBuddyInfoReq;
	}
	
	@Override
	protected TcpEnvelope onPreExecute() {
		MTalkLogWriter.d("SetBlockBuddyTask.onPreExecute()");

		TcpEnvelope rev = new TcpEnvelope.Builder()
			.setType(TcpEnvelope.REQUEST)
			.setMessage(mSetBuddyInfoReq)
			.setMethodId(Session.Contact.SetBuddyInfo)
			.build();
		
		return rev;
	}

	@Override
	protected void onPostExecute(int resultCode, Object sentObject,	Object receivedObject) {
		MTalkLogWriter.d("SetRemoveBuddyTask.onPostExecute()");
		if (receivedObject == null) {
			mUiHandler.sendMessageAtFrontOfQueue(mUiHandler.obtainMessage(HandlerMsgConst.HANDLER_UNKNOWN_ERROR));
			return;
		}
		
		TcpEnvelope resultEnvelope = (TcpEnvelope) receivedObject;
		SetBuddyInfoRes setBuddyInfoRes = (SetBuddyInfoRes) resultEnvelope.getMessage();
		
		MTalkLogWriter.d("SetRemoveBuddyTask ResponseCode : " + setBuddyInfoRes.getResponseCode());
		
		if(setBuddyInfoRes.getResponseCode() == Common.SUCCESS){
			// TODO : 디비작업
			mUiHandler.sendMessageAtFrontOfQueue(mUiHandler.obtainMessage(HandlerMsgConst.HANDLER_REMOVE_BUDDY_SUCCESS));
			BuddyDatabaseHelper.updateBuddyStatus(mContext, mSetBuddyInfoReq);
		} else {
			mUiHandler.sendMessageAtFrontOfQueue(mUiHandler.obtainMessage(HandlerMsgConst.HANDLER_REMOVE_BUDDY_FAIL));
		}
	}

	
}
